package com.ruoyi.business.service.impl;

import com.ruoyi.business.domain.WmsInventoryPlan;
import com.ruoyi.business.mapper.WmsInventoryPlanMapper;
import com.ruoyi.business.mapper.WmsInventoryRecordMapper;
import com.ruoyi.business.service.IWmsInventoryPlanService;
import com.ruoyi.common.utils.BarCodeUtils;
import com.ruoyi.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 盘点计划Service业务层处理
 *
 * @author ruoyi
 * @date 2024-01-21
 */
@Service
public class WmsInventoryPlanServiceImpl implements IWmsInventoryPlanService
{
    @Autowired
    private WmsInventoryPlanMapper wmsInventoryPlanMapper;

    @Autowired
    private WmsInventoryRecordMapper wmsInventoryRecordMapper;

    /**
     * 查询盘点计划
     *
     * @param id 盘点计划主键
     * @return 盘点计划
     */
    @Override
    public WmsInventoryPlan selectWmsInventoryPlanById(Long id)
    {
        WmsInventoryPlan wmsInventoryPlan = wmsInventoryPlanMapper.selectWmsInventoryPlanById(id);
        if (wmsInventoryPlan != null) {
            wmsInventoryPlan.setBarCode(BarCodeUtils.barCodeImageBase64(wmsInventoryPlan.getInventoryNo()));
        }
        return wmsInventoryPlan;
    }

    /**
     * 查询盘点计划列表
     *
     * @param wmsInventoryPlan 盘点计划
     * @return 盘点计划
     */
    @Override
    public List<WmsInventoryPlan> selectWmsInventoryPlanList(WmsInventoryPlan wmsInventoryPlan)
    {
        return wmsInventoryPlanMapper.selectWmsInventoryPlanList(wmsInventoryPlan);
    }

    /**
     * 新增盘点计划
     *
     * @param wmsInventoryPlan 盘点计划
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insertWmsInventoryPlan(WmsInventoryPlan wmsInventoryPlan)
    {
        String no = DateUtils.dateTimeNow(DateUtils.YYYYMMDD);
        wmsInventoryPlan.setInventoryNo(no);
        wmsInventoryPlan.setCreateTime(DateUtils.getNowDate());
        wmsInventoryPlan.setStatus("wait");
        return wmsInventoryPlanMapper.insertWmsInventoryPlan(wmsInventoryPlan);
    }

    /**
     * 修改盘点计划
     *
     * @param wmsInventoryPlan 盘点计划
     * @return 结果
     */
    @Override
    public int updateWmsInventoryPlan(WmsInventoryPlan wmsInventoryPlan)
    {
        wmsInventoryPlan.setInventoryNo(null);
        wmsInventoryPlan.setUpdateTime(DateUtils.getNowDate());
        wmsInventoryPlan.setStatus(null);
        return wmsInventoryPlanMapper.updateWmsInventoryPlan(wmsInventoryPlan);
    }

    /**
     * 批量删除盘点计划
     *
     * @param ids 需要删除的盘点计划主键
     * @return 结果
     */
    @Override
    public int deleteWmsInventoryPlanByIds(Long[] ids)
    {
        int i = wmsInventoryPlanMapper.deleteWmsInventoryPlanByIds(ids);
        wmsInventoryRecordMapper.deleteWmsInventoryRecordByPlanIds(ids);
        return i;
    }

    /**
     * 删除盘点计划信息
     *
     * @param id 盘点计划主键
     * @return 结果
     */
    @Override
    public int deleteWmsInventoryPlanById(Long id)
    {
        return wmsInventoryPlanMapper.deleteWmsInventoryPlanById(id);
    }
}
